package com.tang.dict;

import com.tang.utils.CommonUtils;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

@Getter
@Setter
public class DictParseField {
    DictType type;
    String dictCode;
    String resultField;

    String dictTable;
    String dictKeyColumn;
    String dictTitleColumn;
    Class resultType;
    boolean resultList;

    String dictValue;

    String dictTitle;
    List dictObjects;

    public void callback() {
    }

    public void check(Field field) {
        if (type == DictType.dictCode && (CommonUtils.isEmpty(dictCode) || CommonUtils.isEmpty(dictValue))) {
            throw new RuntimeException("数据字典配置有误: dictCode类型的字典, dictCode, dictValue不能为空, 字段: " + field.toString());
        }

        if (type == DictType.dictTable
                && (CommonUtils.isEmpty(dictTable)
                || CommonUtils.isEmpty(dictKeyColumn)
                || CommonUtils.isEmpty(dictTitleColumn))) {
            throw new RuntimeException("数据字典配置有误: dictTable类型的字典, dictTable, dictKeyColumn, dictTitleColumn不能为空, 字段: " + field.toString());
        }

        if (type == DictType.dictTableObject
                && (CommonUtils.isEmpty(dictTable)
                || CommonUtils.isEmpty(dictKeyColumn)
                || CommonUtils.isEmpty(resultType))) {
            throw new RuntimeException("数据字典配置有误: dictTableObject类型的字典, dictTable, dictKeyColumn, resultType不能为空, 字段: " + field.toString());
        }
    }

    public boolean eq(DictParseField field) {
        return field.type == field.type
            && eq(dictCode, field.dictCode)
            && eq(dictTable, field.dictTable)
            && eq(dictKeyColumn, field.dictKeyColumn)
            && eq(dictTitleColumn, field.dictTitleColumn)
            && eq(dictValue, field.dictValue);
    }

    private boolean eq(String str1, String str2) {
        if(Objects.isNull(str1) && Objects.isNull(str2)) return true;
        if(!Objects.isNull(str1) && str1.equals(str2)) return true;
        return false;
    }
}
